package ingsw2.ii.dp.reader;

import ingsw2.ii.dp.a.MyIterator;
import ingsw2.ii.dp.a.MyList;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class MyReader {

	private BufferedReader br;
	private MyList<String> list = new MyList<String>();

	public MyReader(FileReader in) throws IOException {
		br = new BufferedReader(in);
		this.read();
	}

	public MyReader(String filename) throws IOException {
		br = new BufferedReader(new FileReader(filename));
		this.read();
	}


	protected void read() throws IOException {
		String tmp;
		while((tmp = br.readLine()) != null)
			list.addElement(tmp);
	}

	protected void print(MyPrint printer, MyIterator<String> it){
		while(it.hasMoreElements())
			printer.write(it.nextElement());
		if (printer instanceof MyStatistic) {
			MyStatistic statistic = (MyStatistic) printer;
			statistic.printStatistic(printer);
		}
	}

	public void printForward(MyPrint printer){
		print(printer, list.getForwardIterator());
	}
	public void printBackward(MyPrint printer){
		print(printer, list.getBackwardIterator());
	}
	
}
